package com.zlm.user.partner.service.impl;

import com.zlm.amqp.conf.RabbitConfig;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.NumberConstants;
import com.zlm.common.constants.UserConstants;
import com.zlm.common.dto.userPartner.SysPartnerDto;
import com.zlm.common.entity.SysPartnerDO;
import com.zlm.common.entity.UserTokenDO;
import com.zlm.common.util.DateTimeUtils;
import com.zlm.common.util.TokenUtils;
import com.zlm.huanxin.util.HuanxinUtils;
import com.zlm.user.partner.dao.SysPartnerDao;
import com.zlm.user.partner.dao.UserTokenDao;
import com.zlm.user.partner.service.LoginService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: duyufu
 * @Date: 2019/7/10 13:36
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SysPartnerDao sysPartnerDao;
    @Autowired
    private UserTokenDao userTokenDao;

    @Override
    public SysPartnerDto loginPartner(String mobile) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile",mobile);
        List<SysPartnerDto> sysPartnerList = sysPartnerDao.queryPartner(map);
        if (null != sysPartnerList && sysPartnerList.size() == 1) {
            SysPartnerDto partner = sysPartnerList.get(0);
            SysPartnerDto sysPartnerDto = completPartnerDto(partner);
            UserTokenDO userToken = saveToken(mobile,partner.getPartId());
            sysPartnerDto.setToken(userToken.getToken());
            if (sysPartnerDto.getHxStatus().intValue()!= NumberConstants.NUM_1) {
                String username = UserConstants.USER_TYPE_PARTNER_STRING.concat(partner.getPartId().toString());
                String queueName = RabbitConfig.HX_PART_REGISTER;
                //环信注册
                HuanxinUtils.huanxinRegister(partner.getPartId(),username,queueName);
            }
            return sysPartnerDto;
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
    }

    /**
     * 验证码注册
     * @param mobile
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysPartnerDto activationLogin(String mobile) {
        SysPartnerDO user = new SysPartnerDO();
        user.setMobile(mobile);
        user.setStatus(UserConstants.PARTNER_REVIEW_DEFAULT);
        user.setSourceType(UserConstants.USER_SOURCE_APP);
        user.setCreaterUser(UserConstants.CREATE_USER);
        user.setAlive(1);
        user.setCreatTime(DateTimeUtils.getCurrentDateTime());
        sysPartnerDao.insertPartner(user);
        Integer partId = user.getPartId();
        SysPartnerDto sysPartner = sysPartnerDao.queryPartnerById(partId);
        SysPartnerDto sysPartnerDto = completPartnerDto(sysPartner);
        UserTokenDO userToken = saveToken(mobile,user.getPartId());
        sysPartnerDto.setToken(userToken.getToken());
        String username = UserConstants.USER_TYPE_PARTNER_STRING.concat(partId.toString());
        String queueName = RabbitConfig.HX_PART_REGISTER;
        //环信注册
        HuanxinUtils.huanxinRegister(partId,username,queueName);
        return sysPartnerDto;
    }

    /**
     * 环信注册
     * @param userId
     */
    @Override
    public void huanxinRegister(Integer userId) {
        String username = UserConstants.USER_TYPE_PARTNER_STRING.concat(userId.toString());
        String queueName = RabbitConfig.HX_PART_REGISTER;
        //环信注册
        HuanxinUtils.huanxinRegister(userId,username,queueName);
    }

    @Override
    public void huanxinDisconnect(String userHxId) {
        HuanxinUtils.disconnect(userHxId);
    }

    private SysPartnerDto completPartnerDto(SysPartnerDto partner) {
        if (StringUtils.isEmpty(partner.getPartName())) {
            partner.setPartName(UserConstants.USER_DEFAULT_NAME);
        }
        return partner;
    }


    /**
     * 保存token
     * @param mobile
     * @param partId
     * @return
     */
    private UserTokenDO saveToken(String mobile,Integer partId) {
        String newToken = TokenUtils.createToken(mobile);
        UserTokenDO userToken = new UserTokenDO();
        userToken.setUserId(partId);
        userToken.setToken(newToken);
        userToken.setMobile(mobile);
        userToken.setUserType(UserConstants.USER_TYPE_PARTNER);
        userToken.setAlive(1);
        int token = userTokenDao.getPartnerToken(mobile);
        if (token==0){
            userTokenDao.savePartnerToken(userToken);
        } else {
            userTokenDao.updatePartnerToken(userToken);
        }
        return userToken;
    }

}
